home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP12.ZIP / PATRON / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-07-19  |  35KB  |  1,484 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Modifications for Chapter 12
  4.  *
  5.  * Implementation of the CPatronDoc derivation of CDocument that
  6.  * manages pages for us.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include <memory.h>
  19. #include "patron.h"
  20.  
  21.  
  22.  
  23. /*
  24.  * CPatronDoc::CPatronDoc
  25.  * CPatronDoc::~CPatronDoc
  26.  *
  27.  * Constructor Parameters:
  28.  *  hInst           HINSTANCE of the application.
  29.  */
  30.  
  31. CPatronDoc::CPatronDoc(HINSTANCE hInst)
  32.     : CDocument(hInst)
  33.     {
  34.     m_pPG=NULL;
  35.     m_lVer=VERSIONCURRENT;
  36.     m_pIStorage=NULL;
  37.     m_fPrintSetup=TRUE;
  38.     m_pDropTarget=NULL;
  39.  
  40.     m_cfEmbeddedObject  =RegisterClipboardFormat(CF_EMBEDDEDOBJECT);
  41.     m_cfObjectDescriptor=RegisterClipboardFormat(CF_OBJECTDESCRIPTOR);
  42.  
  43.     //CHAPTER12MOD
  44.     m_cfLinkSource       =RegisterClipboardFormat(CF_LINKSOURCE);
  45.     m_cfLinkSrcDescriptor=RegisterClipboardFormat(CF_LINKSRCDESCRIPTOR);
  46.     m_fShowTypes         =FALSE;
  47.     //End CHAPTER12MOD
  48.     return;
  49.     }
  50.  
  51.  
  52. CPatronDoc::~CPatronDoc(void)
  53.     {
  54.     if (NULL!=m_pDropTarget)
  55.         {
  56.         RevokeDragDrop(m_hWnd);
  57.         CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, FALSE, TRUE);
  58.         m_pDropTarget->Release();
  59.         }
  60.  
  61.     if (NULL!=m_pPG)
  62.         delete m_pPG;
  63.  
  64.     if (NULL!=m_pIStorage)
  65.         m_pIStorage->Release();
  66.  
  67.     CoFreeUnusedLibraries();
  68.     return;
  69.     }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. /*
  76.  * CPatronDoc::FInit
  77.  *
  78.  * Purpose:
  79.  *  Initializes an already created document window.  The client actually
  80.  *  creates the window for us, then passes that here for further
  81.  *  initialization.
  82.  *
  83.  * Parameters:
  84.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  85.  *
  86.  * Return Value:
  87.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  88.  */
  89.  
  90. BOOL CPatronDoc::FInit(LPDOCUMENTINIT pDI)
  91.     {
  92.     //Change the stringtable range to our customization.
  93.     pDI->idsMin=IDS_DOCUMENTMIN;
  94.     pDI->idsMax=IDS_DOCUMENTMAX;
  95.  
  96.     //Do default initialization
  97.     if (!CDocument::FInit(pDI))
  98.         return FALSE;
  99.  
  100.     //Pages are created when we get a ::ULoad later.
  101.     return TRUE;
  102.     }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /*
  110.  * CPatronDoc::FMessageHook
  111.  *
  112.  * Purpose:
  113.  *  Processes WM_SIZE for the document so we can resize the Pages window.
  114.  *
  115.  * Parameters:
  116.  *  <WndProc Parameters>
  117.  *  pLRes           LRESULT FAR * in which to store the return value
  118.  *                  for the message.
  119.  *
  120.  * Return Value:
  121.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  122.  */
  123.  
  124. BOOL CPatronDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  125.     , LPARAM lParam, LRESULT FAR *pLRes)
  126.     {
  127.     UINT        dx, dy;
  128.     RECT        rc;
  129.  
  130.     if (WM_SIZE==iMsg && NULL!=m_pPG)
  131.         {
  132.         dx=LOWORD(lParam);
  133.         dy=HIWORD(lParam);
  134.  
  135.         //Resize the Pages window to fit the new client area of the document
  136.         GetClientRect(hWnd, &rc);
  137.         m_pPG->RectSet(&rc, FALSE);
  138.         }
  139.  
  140.     /*
  141.      * We return FALSE even on WM_SIZE so we can let the default procedure
  142.      * handle maximized MDI child windows appropriately.
  143.      */
  144.     return FALSE;
  145.     }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * CPatronDoc::Clear
  156.  *
  157.  * Purpose:
  158.  *  Sets all contents in the document back to defaults with no filename.
  159.  *
  160.  * Paramters:
  161.  *  None
  162.  *
  163.  * Return Value:
  164.  *  None
  165.  */
  166.  
  167. void CPatronDoc::Clear(void)
  168.     {
  169.     //Completely reset the pages
  170.     m_pPG->FIStorageSet(NULL, FALSE, FALSE);
  171.  
  172.     CDocument::Clear();
  173.     m_lVer=VERSIONCURRENT;
  174.     return;
  175.     }
  176.  
  177.  
  178.  
  179.  
  180. /*
  181.  * CPatronDoc::FDirtyGet
  182.  *
  183.  * Purpose:
  184.  *  Returns the current dirty status of the document.
  185.  *
  186.  * Parameters:
  187.  *  None
  188.  *
  189.  * Return Value:
  190.  *  BOOL            TRUE if the file is clean, FALSE otherwise.
  191.  */
  192.  
  193. BOOL CPatronDoc::FDirtyGet()
  194.     {
  195.     BOOL    fPageDirty;
  196.  
  197.     fPageDirty=m_pPG->FIsDirty();
  198.     return m_fDirty | fPageDirty;
  199.     }
  200.  
  201.  
  202.  
  203.  
  204.  
  205. /*
  206.  * CPatronDoc::Delete
  207.  *
  208.  * Purpose:
  209.  *  Removed the current object from the document.
  210.  *
  211.  * Paramters:
  212.  *  None
  213.  *
  214.  * Return Value:
  215.  *  None
  216.  */
  217.  
  218. void CPatronDoc::Delete(void)
  219.     {
  220.     if (NULL!=m_pPG)
  221.         m_pPG->TenantDestroy();
  222.  
  223.     CoFreeUnusedLibraries();
  224.     return;
  225.     }
  226.  
  227.  
  228.  
  229. /*
  230.  * CPatronDoc::FQueryPrinterSetup
  231.  *
  232.  * Purpose:
  233.  *  Returns whether or not the Printer Setup menu item can be
  234.  *  enabled.  Once you create a tenant in any page, Printer Setup
  235.  *  is voided simply to keep this sample simple, that is, we don't
  236.  *  have to worry about reorganizing potentially large amounts
  237.  *  of layout after we start plopping down objects.
  238.  *
  239.  * Parameters:
  240.  *  None
  241.  *
  242.  * Return Value:
  243.  *  BOOL            TRUE to enable the menu, FALSE otherwise.
  244.  */
  245.  
  246. BOOL CPatronDoc::FQueryPrinterSetup(void)
  247.     {
  248.     return m_fPrintSetup;
  249.     }
  250.  
  251.  
  252.  
  253.  
  254.  
  255. /*
  256.  * CPatronDoc::FQueryObjectSelected
  257.  *
  258.  * Purpose:
  259.  *  Returns whether or not there is an object selected in this
  260.  *  document for Cut, Copy, Delete functions.
  261.  *
  262.  * Parameters:
  263.  *  hMenu           HMENU of the Edit menu.
  264.  *
  265.  * Return Value:
  266.  *  BOOL            TRUE if we have an object, FALSE otherwise.
  267.  */
  268.  
  269. BOOL CPatronDoc::FQueryObjectSelected(HMENU hMenu)
  270.     {
  271.     return m_pPG->FQueryObjectSelected(hMenu);
  272.     }
  273.  
  274.  
  275.  
  276.  
  277.  
  278. /*
  279.  * CPatronDoc::ULoad
  280.  *
  281.  * Purpose:
  282.  *  Loads a given document without any user interface overwriting the
  283.  *  previous contents of the editor.
  284.  *
  285.  * Parameters:
  286.  *  fChangeFile     BOOL indicating if we're to update the window title
  287.  *                  and the filename from using this file.
  288.  *  pszFile         LPSTR to the filename to load.  Could be NULL for
  289.  *                  an untitled document.
  290.  *
  291.  * Return Value:
  292.  *  UINT            An error value from DOCERR_*
  293.  */
  294.  
  295.  
  296. UINT CPatronDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
  297.     {
  298.     RECT        rc;
  299.     LPSTORAGE   pIStorage;
  300.     HRESULT     hr;
  301.     CLSID       clsID;
  302.     DWORD       dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  303.  
  304.     if (NULL==pszFile)
  305.         {
  306.         //Create a new temp file.
  307.         hr=StgCreateDocfile(NULL, dwMode | STGM_CREATE | STGM_DELETEONRELEASE
  308.             , 0, &pIStorage);
  309.  
  310.         //Mark this as one of our class since we check with ReadClassStg below.
  311.         if (SUCCEEDED(hr))
  312.             WriteClassStg(pIStorage, CLSID_PatronPages);
  313.         }
  314.     else
  315.         {
  316.         hr=StgOpenStorage(pszFile, NULL, dwMode, NULL, 0, &pIStorage);
  317.         }
  318.  
  319.     if (FAILED(hr))
  320.         return DOCERR_COULDNOTOPEN;
  321.  
  322.     //Check if this is our type of file and exit if not.
  323.     hr=ReadClassStg(pIStorage, &clsID);
  324.  
  325.     if (FAILED(hr) || !IsEqualCLSID(clsID, CLSID_PatronPages))
  326.         {
  327.         pIStorage->Release();
  328.         return DOCERR_READFAILURE;
  329.         }
  330.  
  331.     //Attempt to create our contained Pages window.
  332.     m_pPG=new CPages(m_hInst, m_cf);
  333.     GetClientRect(m_hWnd, &rc);
  334.  
  335.     if (!m_pPG->FInit(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_PAGES, NULL))
  336.         {
  337.         pIStorage->Release();
  338.         return DOCERR_READFAILURE;
  339.         }
  340.  
  341.     if (!m_pPG->FIStorageSet(pIStorage, FALSE, (BOOL)(NULL==pszFile)))
  342.         {
  343.         pIStorage->Release();
  344.         return DOCERR_READFAILURE;
  345.         }
  346.  
  347.     //Open the window up for drag-drop
  348.     m_pDropTarget=new CDropTarget(this);
  349.  
  350.     if (NULL!=m_pDropTarget)
  351.         {
  352.         m_pDropTarget->AddRef();
  353.         RegisterDragDrop(m_hWnd, (LPDROPTARGET)m_pDropTarget);
  354.         CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, TRUE, FALSE);
  355.         }
  356.  
  357.     m_pIStorage=pIStorage;
  358.     Rename(pszFile);
  359.  
  360.     //Do initial setup if this is a new file, otherwise Pages handles things.
  361.     if (NULL==pszFile)
  362.         {
  363.         //Go initialize the Pages for the default printer.
  364.         if (!PrinterSetup(NULL, TRUE))
  365.             return DOCERR_COULDNOTOPEN;
  366.  
  367.         //Go create an initial page.
  368.         m_pPG->PageInsert(0);
  369.         }
  370.     else
  371.         m_fPrintSetup=FALSE;    //Ca